<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.socket.io/4.8.1/socket.io.esm.min.js"></script>
  </head>
  <body>
    fas
    <button id="sendBtn">发送消息</button>
  </body>
  <script type="module">
    import { io } from 'https://cdn.socket.io/4.8.1/socket.io.esm.min.js';
    const search = window.location.search;
    const query = search.substring(1);
    // console.log(query);
    const queryParams = new URLSearchParams(query);
    // console.log();
    document.getElementById('sendBtn').addEventListener('click', () => {
      socket.emit('privateMessage', {
        toUserId: queryParams.get('id'),
        message: '你好，' + queryParams.get('id'),
      });
    });

    const socket = io('http://localhost:3001/chat', {
      query: {
        token: '123',
        userId: queryParams.get('id'),
      },
    });
    // console.log(queryParams.get('id'));
    socket.on('connect', () => {
      console.log('已连接到 WebSocket 服务');
    });
    // socket.emit('sendMessage', { user: '小明', message: 'Hello NestJS!' });
    // socket.on('receiveMessage', (data) => {
    //   console.log('收到消息：', data);
    // });
    // 单聊

    socket.on('receivePrivateMessage', (data) =>
      console.log('收到私聊：', data),
    );

    // 加入房间
    // socket.emit('joinRoom', { room: 'group1' });
    // socket.emit('roomMessage', { room: 'group1', message: '大家好！' });
    // socket.on('receiveRoomMessage', (data) => console.log('收到群聊：', data));

    // 广播
    // socket.emit('broadcast', { message: '全体注意！' });
    // socket.on('receiveBroadcast', (data) => console.log('收到广播：', data));
  </script>
</html>
